import axios from 'axios'
import store from '@/store/index'
import {baseURL} from './env'
const Axios = axios.create({
    baseURL: baseURL+'/api'
})

Axios.interceptors.request.use((data:any)=>{

    const token =store.state.access_token
    //添加一个头部自定义字段
    data.headers['Authorization']= 'Bearer ' + token
    return data
})

//状态码字典
const dict =[201,200]
Axios.interceptors.response.use((res:any) => {
    //如果是200直接返回data
    if (dict.includes(res.status) ) {
        return res.data
    } else {
        return Promise.reject(new Error(res.data.message || '请求失败了'))
    }
})
export default Axios